/// 问答问题数据模型
/// 包含用户发布的问题信息
class QuestionEntity {
  final String questionId;
  final String authorId; // 提问者ID
  final String title;
  final QAMediaType mediaType;
  final String? mediaUrl;
  final String? thumbnailUrl; // 视频封面图
  final DateTime publishTime;
  final int likeCount;
  final int answerCount;
  final List<String> likedUserIds; // 点赞用户ID列表

  const QuestionEntity({
    required this.questionId,
    required this.authorId,
    required this.title,
    required this.mediaType,
    this.mediaUrl,
    this.thumbnailUrl,
    required this.publishTime,
    this.likeCount = 0,
    this.answerCount = 0,
    this.likedUserIds = const [],
  });

  /// 从JSON创建问题对象
  factory QuestionEntity.fromJson(Map<String, dynamic> json) {
    return QuestionEntity(
      questionId: json['questionId'] as String,
      authorId: json['authorId'] as String,
      title: json['title'] as String,
      mediaType: QAMediaType.values.firstWhere(
        (e) => e.toString() == 'QAMediaType.${json['mediaType']}',
      ),
      mediaUrl: json['mediaUrl'] as String?,
      thumbnailUrl: json['thumbnailUrl'] as String?,
      publishTime: DateTime.parse(json['publishTime'] as String),
      likeCount: json['likeCount'] as int? ?? 0,
      answerCount: json['answerCount'] as int? ?? 0,
      likedUserIds:
          (json['likedUserIds'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'questionId': questionId,
      'authorId': authorId,
      'title': title,
      'mediaType': mediaType.toString().split('.').last,
      'mediaUrl': mediaUrl,
      'thumbnailUrl': thumbnailUrl,
      'publishTime': publishTime.toIso8601String(),
      'likeCount': likeCount,
      'answerCount': answerCount,
      'likedUserIds': likedUserIds,
    };
  }

  /// 复制问题对象并修改部分属性
  QuestionEntity copyWith({
    String? questionId,
    String? authorId,
    String? title,
    QAMediaType? mediaType,
    String? mediaUrl,
    String? thumbnailUrl,
    DateTime? publishTime,
    int? likeCount,
    int? answerCount,
    List<String>? likedUserIds,
  }) {
    return QuestionEntity(
      questionId: questionId ?? this.questionId,
      authorId: authorId ?? this.authorId,
      title: title ?? this.title,
      mediaType: mediaType ?? this.mediaType,
      mediaUrl: mediaUrl ?? this.mediaUrl,
      thumbnailUrl: thumbnailUrl ?? this.thumbnailUrl,
      publishTime: publishTime ?? this.publishTime,
      likeCount: likeCount ?? this.likeCount,
      answerCount: answerCount ?? this.answerCount,
      likedUserIds: likedUserIds ?? this.likedUserIds,
    );
  }

  @override
  String toString() {
    return 'QuestionEntity(questionId: $questionId, authorId: $authorId, title: $title)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is QuestionEntity && other.questionId == questionId;
  }

  @override
  int get hashCode => questionId.hashCode;
}

/// 问答媒体类型枚举
enum QAMediaType {
  image, // 图片
  video, // 视频
}
